package handler;

import beans.Plan;
import beans.ResultPlan;
import beans.Task;
import beans.UpdataPlan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import service.employeeService;
import java.util.List;

@Controller
@RequestMapping("/emp")
public class employeeHandler {
    @Autowired
    private employeeService emp;

    @RequestMapping("/empTask")
    public String empTask(Integer id, Model model){
        List<Task> tasks = emp.selectTaskByEmpId(id);
        model.addAttribute("tasks",tasks);

        return "employee/management_task";
    }

    @RequestMapping("/taskPlan")
    public String taskPlan(Integer id,Model model){
        List<Plan> plans=emp.selectPlanByTaskId(id);
        Task task=emp.selectTaskById(id);
        model.addAttribute("plans",plans);
        model.addAttribute("task",task);

        return "employee/queryPlanByTask";
    }

    @RequestMapping("/openNewPlanPage")
    public String openNewPlanPage(Integer task_id,Model model){
        System.out.println("openNewPlanPage : task_id "+task_id);
        model.addAttribute("task_id",task_id);
        return "employee/new_plan";
    }

    @RequestMapping("/creatPlan")
    public String creatPlan(Plan plan,Model model){
        Integer task_id=plan.getTask_id();
        System.out.println(task_id);
        emp.addPlan(plan);
        return this.taskPlan(task_id,model);
    }

    @RequestMapping("/deletePlanById")
    public String deletePlanById(Integer plan_id,Integer task_id,Model model){
        emp.deletePlanById(plan_id);
        model.addAttribute("task_id",task_id);

        return this.taskPlan(task_id,model);
    }

    @RequestMapping("/feedBack")
    public String feedBack(Integer plan_id,Model model){
        Plan plan=emp.selectPlanById(plan_id);
        model.addAttribute("plan",plan);

        return "employee/ticking_task";
    }

    @RequestMapping("/updatePlanById")
    public String updatePlanById(UpdataPlan plan, Model model){
        emp.updatePlanById(plan);
        System.out.println("updatePlan "+plan);
        return this.taskPlan(plan.getTask_id(),model);
    }

    @RequestMapping("/openQuery")
    public String openQuery(Integer id,Model model){
        List<Task> tasks=emp.selectTaskByEmpId(id);
        model.addAttribute("tasks",tasks);
        return "employee/query_plan";
    }
    @RequestMapping("/queryLikePlan")
    public String queryLikePlan(ResultPlan plan,Model model){
        List<Integer> taskIds=emp.selectTaskId(plan.getUserId());
        List<Integer> planIds=emp.selectPlanId(taskIds);
        plan.setPlanIds(planIds);
        List<Plan> plans=emp.selectPlanByLike(plan);
        model.addAttribute("plans",plans);
        return "employee/query_like_plan";
    }

}
